#!/bin/sh
# PCP QA Test No. 356
#
# segv with derived metrics
#
# Copyright (c) 2010 Ken McDonell.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

_cleanup()
{
    _restore_auto_restart pmcd
    rm -f $tmp.*
}

status=0	# success is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

cat <<End-of-File >$tmp.config
bad_in_pkts = network.interface.in.errors + network.interface.in.drops
End-of-File

_filter()
{
    _filter_pmie_log \
    | sed \
	-e '/^\[TIMESTAMP]/'"s/ `hostname`/ HOST/" \
	-e '/^\[TIMESTAMP]/'"s/ local:/ HOST/" \
	-e '/expr_1:.*?/d' \
	-e '/expr_1: [0-9. ]*/s/ .*/ value(s)/' \
	-e 's/Connection reset by peer/IPC protocol failure/' \
	-e '/^  *$/d' \
	-e '/^$/d' \
    | $PCP_AWK_PROG '
skip == 0 && /expr_1/	{ print; print "..."; print ""; skip = 1; next }
skip == 1 && /expr_1/	{ next }
skip == 1 &&  NF == 0	{ next }
			{ print; skip = 0 }'
}

# real QA test starts here

export PCP_DERIVED_CONFIG=$tmp.config
echo "bad_in_pkts;" | pmie -t 1 -T+15 -v >$tmp.out 2>&1 &

sleep 3

unset PCP_DERIVED_CONFIG
_stop_auto_restart pmcd
if ! _service pmcd restart 2>&1; then _exit 1; fi | _filter_pcp_restart
_wait_for_pmcd || _exit 1

wait

cat $tmp.out >$seq_full
# Especially on slow VMs (like bozo-vm) we may need to dodge warnings
# from pmie's rule scheduler around pmcd reconnection ... the awk
# script does this.
#
cat <$tmp.out \
| $PCP_AWK_PROG '
skip == 1 && /^[^ ]/		{ skip = 0 }
/^run: schedule eval/		{ skip = 1 }
/^sleepTight: negative/		{ skip = 1 }
/^Last sleepTight until:/	{ skip = 1 }
/^This sleepTight\() entry:/	{ skip = 1 }
/^Harvest children done:/	{ skip = 1 }
/^Want sleepTight until:/	{ skip = 1 }
/Task dump @/			{ skip = 1 }
skip == 0			{ print }' \
| _filter

# success, all done
exit
